# encoding: utf-8

import xadmin
from xadmin import views
from .models import *
from .forms import *
from usercenter.models import Users
#from .models import xbRoom
from xadmin.layout import Main, TabHolder, Tab, Fieldset, Row, Col, AppendedText, Side
from xadmin.plugins.inline import Inline
from xadmin.plugins.batch import BatchChangeAction
from django.utils.translation import ugettext_lazy as _
import logging
logger = logging.getLogger(__name__)

def getList():
	tall=Test2.objects.all()
	tlist=[]
	for item in tall:
		oneItem=(item.id,item)
		tlist.append(oneItem)


	return tuple(tlist)

def getTuple():
	tall=Test2.objects.all()

	return tuple(tall)


ROOM_STATUS = (
    (0, u"可预定"),
    (1, u"未开放"),
)
ROOM_STATUS2 = (
    (0, u"asd定"),
    (1, u"未dddd放"),
)
class Test1Admin(object):
	#form = Test1Form
	
	wizard_form_list = [
		('Form1', ('main1','main2',)),
		('Form2', ('name',)),
		#(_(u"Widget Params"), {'callback':"get_widget_params_form", 'convert': "convert_widget_params"}),
		#('Thread Form', ('customer_id',))
	]
	search_fields = ['name']
	relfield_style = 'fk-ajax'

	def formfield_for_dbfield(self, db_field, **kwargs):
		#logger.info('----------------')
		#logger.info(db_field.name)
		#logger.info(self.request.GET)
		#logger.info(self.request.POST)
		#logger.info('----------------')
		if db_field.name == 'main2':
			#widgets = widget_manager.get_widgets(self.request.GET.get('page_id', ''))
			#form_widget = WidgetTypeSelect(widgets)
			return forms.ChoiceField(choices=ROOM_STATUS2 , label=_(u'main2_label'))
		if 'name' == db_field.name :
			main1value=self.request.POST.get(u'step_0-main1', None)
			if main1value is not None:
				if main1value == '1':
					return forms.ModelChoiceField(queryset=Test2.objects.filter(id=1))
			#return forms.ChoiceField(choices=getList() , label=_(u'name_label'))
			#return  forms.ChoiceField(choices=getTuple(),label=_(u'name_la'))
			#return forms.ModelMultipleChoiceField(queryset=Test2.objects.all())
			return forms.ModelChoiceField(queryset=Test2.objects.all())

		field = super(Test1Admin, self).formfield_for_dbfield(db_field, **kwargs)
		return field

	#def save_forms(self):
	#	self.message_user('save_forms')


	#def _done(self):
	#	logger.info('--------_done--------')

	def save_models(self):
		#return 
		logger.info('--------save_models--------')
		#logger.info(self.new_obj)
		#logger.info(self.request.GET)

		logger.info(self.request.POST)
		logger.info('----------------')
		if self.request.POST.get(u'step_0-main1', None) is None:
			self.message_user('xxxxx')
			logger.info(self.new_obj)
			return 
		else:
			self.message_user('yyyy')
		#self.new_obj.save()
		super(Test1Admin, self).save_models()




xadmin.site.register(Test1,Test1Admin)

class Test2Admin(object):
    pass


xadmin.site.register(Test2,Test2Admin)



class Test3Admin(object):
    pass


xadmin.site.register(Test3,Test3Admin)
